package com.bluedot.www.core.service;

import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.FamilyNumber;
import com.bluedot.www.core.utils.DbUtil;

import java.util.List;

/**
 * @author ZhouXuan
 * @version 1.0
 * @date 2021/9/3 10:21
 */
public class FamilyNumberService {

    private static final String FAMILY_NUMBER_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.FamilyNumberMapper.";

    public static Object execute(String methodName, Object[] param) {
        FamilyNumberService familyNumberService = new FamilyNumberService();
        Object result = null;
        switch (methodName) {
            case "insertFamilyNumbers":
                result = familyNumberService.insertFamilyNumbers((List<FamilyNumber>) param[0], (Long) param[1]);
                break;
            case "updateFamilyNumbers":
                result = familyNumberService.updateFamilyNumbers((List<FamilyNumber>) param[0]);
                break;
            default:
                throw new RuntimeException("Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }

    /**
     * 保存家庭成员信息
     *
     * @param familyNumbers:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/3 10:21
     */
    private boolean insertFamilyNumbers(List<FamilyNumber> familyNumbers, Long customerId) {
        // 添加家庭成员信息
        boolean flag = true;
        for (FamilyNumber familyNumber : familyNumbers) {
            // 设置家庭成员主键
            familyNumber.setId(DbUtil.generateId());

            // 设置家庭成员创建时间和更新时间
            familyNumber.setGmtCreate(DbUtil.getCurrentTime());
            familyNumber.setGmtModified(DbUtil.getCurrentTime());

            //在这里设置家庭成员所在的客户编号
            familyNumber.setCustomerId(customerId);

            Integer count = (Integer) Execute.execute(Execute.UPDATE_TYPE, FAMILY_NUMBER_MAPPER_ADDRESS + "insertFamilyNumber", new Object[]{familyNumber});

            boolean b = count > 0 ? true : false;

            // 如果家庭成员添加失败，则标志置为false，并跳出循环
            if (!b) {
                flag = false;
                break;
            }
        }

        return true;
    }

    /**
     * 修改家庭成员信息
     *
     * @param familyNumbers:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/3 16:34
     */
    public boolean updateFamilyNumbers(List<FamilyNumber> familyNumbers) {
        // 更新家庭成员信息
        boolean flag = true;
        for (FamilyNumber familyNumber : familyNumbers) {

            Integer count = (Integer) Execute.execute(Execute.UPDATE_TYPE, FAMILY_NUMBER_MAPPER_ADDRESS + "updateFamilyNumber", new Object[]{familyNumber});
            // 如果家庭成员删除失败，则标志置为false，并跳出循环
            if (!(count > 0)) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}
